<?php


namespace app\api\controller\v1;

use app\api\exception\BaseException;
use app\api\validate\AccountScene;
use app\api\validate\ClassScene;

use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Db;

/**
 * Class AccountClass
 * @package app\api\controller\v1
 * 用户分类
 */
class AccountClass extends BaseException
{


    /**
     * 不需要登录方法
     */
    protected $noAuth = ['publicClassList'];


    /**
     * 【超级管理员】
     *  查询分类列表 翻页
     * 员工、财务、老板
     */
    public function getAccountClassList()
    {
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,name,data_state,create_time,update_time";
        $search = [
        ];
        /**
         * 单一模糊查询开始
         */
        //名称模糊查询
        if (isset($data["name"]) && !empty($data["name"])) {
            $search[] = ["name", "like", "%" . $data["name"] . "%"];
        }

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        //  if (isset($data["data_state"]) && $data["data_state"] != '') {
        array_push($search, ["data_state", "=", 1]);
        //}
        $classlist = Db::name("account_class")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select()->toArray();
        $count = Db::name("account_class")->where($search)->count();
        foreach ($classlist as $key => $value){
            $brandList = Db::name("account_position")->where(["class_id" => $classlist[$key]["id"]])->select()->toArray();
            $classlist[$key]["children"] = $brandList;
        }
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $classlist
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);

    }
    /**
     * 【公共接口】
     *  数据下拉框
     */
    public function publicClassList()
    {
        $classList = Db::name("account_class")->where(["data_state" => 1])->column("id,name");
        foreach ($classList as $key => $value){
            $check = Db::name("account_position")->where(["class_id" => $value["id"],"data_state" => 1])->find();
            if (empty($check)){
                unset($classList[$key]);
            }

        }
        return self::Success($classList, "获取成功", 200);
    }

    /**
     * 【超级管理员】
     *  添加分类
     * 员工、财务、老板
     */
    public function addAccountClass()
    {
        $request = Request::param();
        try {
            validate(ClassScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $insertData = [
            "name" => $request["name"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("account_class")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     * 【超级管理员】
     * 假删除 通过数据下来筛选
     * 更改分类的数据状态
     * 如果删除的话 需更新所有所属分类的用户的分类字段
     */
    public function updateAccountClassState()
    {
        $request = Request::param();
        try {
            validate(ClassScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("account_class")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("account_class")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '分类不存在', 10002);
        }

    }

    /**
     * 【超级管理员】
     * 更改分类数据
     * 更改分类的数据状态
     * 如果删除的话 需更新所有所属分类的用户的分类字段
     */
    public function updateAccountClassInfo()
    {
        $request = Request::param();
        try {
            validate(ClassScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check = Db::name("account_class")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "name" => $request["name"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("account_class")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '分类不存在', 10002);
        }
    }

}